package org.example.struct.array2;

/**
 * @author liyishan
 * @date 2024/5/9 10:06
 * @apiNote
 */

public class Test02105MinimumRefill {

    public int minimumRefill(int[] plants, int capacityA, int capacityB) {
        int cnt = 0;
        int totalA = capacityA, totalB = capacityB;
        int aSize = 0, bSize = plants.length - 1;
        while (aSize < plants.length && bSize >= 0 && aSize <= bSize) {
            if (aSize != bSize) {
                if (capacityA >= plants[aSize]) {
                    capacityA -= plants[aSize];
                } else {
                    cnt++;
                    capacityA = totalA - plants[aSize];
                }
                if (capacityB >= plants[bSize]) {
                    capacityB -= plants[bSize];
                } else {
                    cnt++;
                    capacityB = totalB - plants[bSize];
                }
                aSize++;
                bSize--;
            } else {
                if (capacityA >= plants[aSize]) {
                    capacityA -= plants[aSize];
                    aSize++;
                } else if (capacityB >= plants[bSize]) {
                    capacityB -= plants[bSize];
                    bSize--;
                } else {
                    cnt++;
                    capacityA = totalA - plants[aSize];
                    aSize++;
                }
            }
        }
        return cnt;
    }
}
